package per.sunmes.lesrb.actor;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

import per.sunmes.lesrb.util.DU;

public class HpBar extends Actor {

	public enum BarType {
		Vertical, Horizon
	}

	protected BarType type;

	protected int maxLength;
	protected float max;
	protected float targetValue;
	protected float currentValue;
	protected float changeValue;
	protected TextureRegion region;
	protected float percent;

	protected float animationDelay = 1;

	public HpBar(TextureRegion region, BarType type) {
		this.type = type;
		TextureRegion tr = new TextureRegion(region);
		this.region = region;
		if (type == BarType.Horizon) {
			maxLength = tr.getRegionWidth();
		} else if (type == BarType.Vertical) {
			maxLength = tr.getRegionHeight();
		}
		setSize(region.getRegionWidth(), region.getRegionHeight());
		setMaxValue();
		this.region = tr;
	}

	public void setMaxValue(float max, boolean isResetTarget) {
		this.max = max;
		if (isResetTarget) {
			this.targetValue = max;
			this.currentValue = max;
		}

		percent = currentValue / max;
	}

	/**
	 * max=1;
	 */
	public void setMaxValue() {
		setMaxValue(1, true);
	}

	public void setAnimationDelay(float dt) {
		animationDelay = dt;
	}

	public float getPercent() {
		return percent;
	}

	public void updateBar(float delta) {
		if (changeValue == 0) {
			percent = currentValue / max;
			return;
		}
		if (max == 0) {
			max = 1;
			targetValue = 1;
		}
		if (animationDelay > 0) {
			currentValue += changeValue * (delta / animationDelay);
		} else {
			currentValue += changeValue;
		}
		if (changeValue < 0) {
			if (currentValue <= targetValue) {
				changeValue = 0;
				currentValue = targetValue;
			}
		} else { // >0 ///no need check ==0
			if (currentValue >= targetValue) {
				changeValue = 0;
				currentValue = targetValue;
			}
		}

		percent = currentValue / max;
	}

	@Override
	public void act(float delta) {
		super.act(delta);
		updateBar(delta);
	}

	/**
	 * 
	 * @param percent
	 *            0-1f
	 */
	public void setPercent(float percent, boolean immediately) {
		if (percent > 1) {
			percent = 1;
		}
		if (percent < 0) {
			percent = 0;
		}
		targetValue = max * percent;
		if (immediately) {
			currentValue = targetValue;
			percent = currentValue / max;
		}
		changeValue = targetValue - currentValue;

		if (immediately) {
			updateBar(animationDelay);
		}
	}

	public void setRegion(TextureRegion region) {
		this.region = region;
	}

	public TextureRegion getRegion() {
		return region;
	}

	public void setPercent(float percent) {
		setPercent(percent, false);
	}

	public void setCurrentValue(float current) {
		setCurrentValue(current, false);
	}

	public void setCurrentValue(float current, boolean immediately) {
		if (current > max) {
			current = max;
		}
		if (current < 0) {
			current = 0;
		}
		targetValue = current;
		if (immediately) {
			currentValue = targetValue;
			percent = currentValue / max;
		}
		changeValue = targetValue - currentValue;
	}

	public float getMaxValue() {
		return max;
	}

	public float getCurrentValue() {
		return targetValue;
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		if (percent == 0) {
			return;
		}
		if (type == BarType.Horizon) {
			DU.drawTextureCustomRect(batch, parentAlpha, region, this, getX(), getY(), getWidth() * percent,
					getHeight(), region.getRegionX(), region.getRegionY(), (int) (region.getRegionWidth() * percent),
					region.getRegionHeight());
		} else if (type == BarType.Vertical) {
			DU.drawTextureBaseDownHeightPercent(batch, parentAlpha, region, this, getX(), getY(), getWidth(),
					getHeight(), percent);
		}

	}

}
